/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-04-29     Administrator       the first version
 */
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-08-03    chenyong      the first version
 */

#include <rtthread.h>
#include <webclient.h>
#include "cJSON.h"
#include "http.h"

#define GET_HEADER_BUFSZ               512
#define GET_RESP_BUFSZ                 1024

#define GET_LOCAL_URI       "http://api.openweathermap.org/data/2.5/weather?q=chengdu&APPID=ca112e0e80de815ba4538bb296b507e0"

static uint8_t get_weather_flag = 0;
int weather_temp;
int weather_humi;
int weather_sky;

char weather_rec_info[GET_RESP_BUFSZ];
static void cjson_cover_data(const char* value);

void weather_date_copy(int *temp, int *humi, int* sky)
{
    if (get_weather_flag == 0) {
        return;
    }
    cjson_cover_data(weather_rec_info);
    *temp = weather_temp;
    *humi = weather_humi;
    *sky = weather_sky;
}

enum weathet_flag
{
    WEATHER_SUN = 1,
    WEATHER_CLOUD,
    WEATHER_RAIN
};

static void cjson_cover_data(const char* value)
{
    if (value == NULL)return;

    char* weather = strstr(value, "\"main\":");
    char* temp = strstr(value, "\"temp\":");
    char* humi = strstr(value, "\"humidity\":");

    char temp_arr[20];
    char humi_arr[20];
    char weather_arr[20];
    char weather_info_sky[20];

    if (weather != NULL)
    {
        strncpy(weather_arr, weather, 14);
        strtok(weather_arr, ":");

        strcpy(weather_info_sky, strtok(NULL, ":"));
       // printf("weather--> %s\r\n",weather_info_sky);

        if (strstr(weather_info_sky, "Clear") != NULL)
        {
            weather_sky = WEATHER_SUN;
            //rt_kprintf("weather_flag--> %s\r\n","Clear");
        }
        if (strstr(weather_info_sky, "Clouds") != NULL)
        {
            weather_sky = WEATHER_CLOUD;
            //rt_kprintf("weather_flag--> %s\r\n","Clouds");
        }
        if (strstr(weather_info_sky, "Rain") != NULL)
        {
            weather_sky = WEATHER_RAIN;
            //rt_kprintf("weather_flag--> %s\r\n","Rain");
        }
    }

    if (temp != NULL)
    {
        strncpy(temp_arr, temp,13);
        strtok(temp_arr,":");
        sscanf(strtok(NULL,":"),"%d", &weather_temp);
        //rt_kprintf("temp_value --> %d \r\n", weather_temp);
    }
    if (humi != NULL) {
        strncpy(humi_arr, humi,13);
        strtok(humi_arr,":");
        sscanf(strtok(NULL,":"),"%d", &weather_humi);
        //rt_kprintf("humi_value --> %d \r\n", weather_humi);
    }
}

/* send HTTP GET request by common request interface, it used to receive longer data */
static int webclient_get_comm(const char *uri)
{
    struct webclient_session* session = RT_NULL;
    int index, ret = 0;
    int bytes_read, resp_status;
    int content_length = -1;

    /* create webclient session and set header response size */
    session = webclient_session_create(GET_HEADER_BUFSZ);
    if (session == RT_NULL)
    {
        ret = -RT_ENOMEM;
        goto __exit;
    }

    /* send GET request by default header */
    if ((resp_status = webclient_get(session, uri)) != 200)
    {
        rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
        ret = -RT_ERROR;
        goto __exit;
    }

    rt_kprintf("webclient get response data: \n");

    content_length = webclient_content_length_get(session);
    if (content_length < 0)
    {
        rt_kprintf("webclient GET request type is chunked.\n");
        do
        {
            bytes_read = webclient_read(session, (void *)weather_rec_info, GET_RESP_BUFSZ);
            if (bytes_read <= 0)
            {
                break;
            }

            for (index = 0; index < bytes_read; index++)
            {
                rt_kprintf("%c", weather_rec_info[index]);
            }
        } while (1);

        rt_kprintf("\n");
    }
    else
    {
        int content_pos = 0;
        do
        {
            bytes_read = webclient_read(session, (void *)weather_rec_info,
                    content_length - content_pos > GET_RESP_BUFSZ ?
                            GET_RESP_BUFSZ : content_length - content_pos);
            if (bytes_read <= 0)
            {
                break;
            }
//            for (index = 0; index < bytes_read; index++)  /*打印收到的信息*/
//            {
//                rt_kprintf("%c", buffer[index]);
//            }
            content_pos += bytes_read;
        } while (content_pos < content_length);
        get_weather_flag = 1;
        rt_kprintf("read_over,start_cover....conut : %d \r\n", content_pos);
        rt_kprintf("\n");
    }

__exit:
    if (session)
    {
        webclient_close(session);
    }

    return ret;
}



int get_weather(void)
{
    char* uri = GET_LOCAL_URI;
//    uri = web_strdup(GET_LOCAL_URI);
//    if(uri == RT_NULL)
//    {
//        rt_kprintf("no memory for create get request uri buffer.\n");
//        return -RT_ENOMEM;
//    }
    return webclient_get_comm(uri);
}

